package com.example.administrator.x3picturetransliteration.Base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.x3picturetransliteration.R;
import com.example.administrator.x3picturetransliteration.View.SwipeLayout.SwipeLayoutManager;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.subscriptions.CompositeSubscription;

/**
 * 项目Fragment的基类
 * 2018/10/9.
 */


public abstract class BaseFragment extends Fragment {

    public CompositeSubscription mCompositeSubscriptions; //管理rxjava的订阅关系

    private Unbinder unbinder; //黄油刀

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(intiLayout(), container, false); //生成布局view

        mCompositeSubscriptions = new CompositeSubscription(); //实例化CompositeSubscription
        unbinder= ButterKnife.bind(this,view); //注册黄油刀

        // 空出状态栏高度
        if (view.findViewById(R.id.StatusBarTextView) != null){

            int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                ((TextView)view.findViewById(R.id.StatusBarTextView)).setHeight(this.getResources().getDimensionPixelSize(resourceId));
            }
        }

        operation(view); //向子类提供布局的view

        return view;
    }


    /** 设置布局 */
    protected abstract int intiLayout();

    /** 获得绑定的view */
    public abstract void operation(View view);

    /** ButterKnife解绑 */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mCompositeSubscriptions.unsubscribe();
        unbinder.unbind();

    }


    @Override
    public void onResume() {
        super.onResume();

        onHiddenChanged(!isVisible());

    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if(hidden){

        }else {

            SwipeLayoutManager.getInstance().closeCurrentLayout();
            SwipeLayoutManager.getInstance().clearCurrentLayout();
            LazyLoad();
        }

    }

    /** 懒加载 */
    public abstract void LazyLoad();

}
